package com.xlzcx.xltz.service.a_standing;

import com.xlzcx.xltz.dao.AStandingMapper;
import com.xlzcx.xltz.dao.AUserMapper;
import com.xlzcx.xltz.entity.AStanding;
import com.xlzcx.xltz.entity.AUser;
import com.xlzcx.xltz.entity.vo.AStandingVo;
import com.xlzcx.xltz.utils.IpUtil;
import com.xlzcx.xltz.utils.R;
import com.xlzcx.xltz.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 作者: 李咨辰
 * 文件名: AStandingServiceImpl.java
 * 路径: com.xlzcx.xltz.service.a_standing
 * 时间: 2024-11-18 13:03
 * 版本: 1.0
 * 描述: TODO 用户实名信息
 **/
@Service
public class AStandingServiceImpl implements AStandingService {

    /**
     * 调用dao层
     */
    @Resource
    private AStandingMapper aStandingMapper;
    /**
     * 调用用户dao层
     */
    @Resource
    private AUserMapper aUserMapper;


    /**
     * 添加实名认证
     *
     * @param aStanding 实名
     * @param request   用于获取ip
     * @return 成功返回true
     */
    @Override
    public R addStanding(AStandingVo aStanding, HttpServletRequest request) {
        AStanding isExist = aStandingMapper.isExist(aStanding.getIdNumber());
        Date date = new Date();
        AStanding addStanding = new AStanding();
        if (isExist == null) {
            addStanding.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
            addStanding.setName(aStanding.getName());
            addStanding.setIdNumber(aStanding.getIdNumber());
            addStanding.setCredit(300);
            addStanding.setEducational(0);
            addStanding.setProfessional(0);
            addStanding.setCreateTime(date);
            addStanding.setUpdateTime(date);
            int i = aStandingMapper.addStanding(addStanding);
            if (i == 0) {
                return R.error("网络异常");
            }
        } else {
            addStanding.setId(isExist.getId());
            addStanding.setName(isExist.getName());
            addStanding.setIdNumber(isExist.getIdNumber());
            addStanding.setCredit(isExist.getCredit());
            addStanding.setEducational(isExist.getEducational());
            addStanding.setProfessional(isExist.getProfessional());
        }
        AUser aUser = new AUser();
        aUser.setId(aStanding.getUserId());
        aUser.setStandingId(addStanding.getId());
        aUser.setUpdateTime(date);
        aUser.setUpdateIp(IpUtil.getIpByHttpServletRequest(request));
        int i = aUserMapper.updateUser(aUser);
        if (i > 0) {
            addStanding.setCreateTime(null);
            addStanding.setUpdateTime(null);
            return R.ok().put("data", addStanding);
        }
        return R.error("网络异常");
    }
}
